<?php

namespace App\Http\Admin\Order\Models;

use Framework\BaseClass\Http\Admin\Model;

use App\Eloquent\Ygt\Order as OrderModel;
use App\Eloquent\Ygt\OrderPre as OrderPreModel;
use App\Eloquent\Ygt\OrderProcess as OrderProcessModel;
use App\Eloquent\Ygt\OrderProcessCourse as OrderProcessCourseModel;
use App\Eloquent\Ygt\DepartmentUser as DepartmentUserModel;
use App\Eloquent\Ygt\OrderType as OrderTypeModel;
use App\Eloquent\Ygt\Process as ProcessModel;

use App\Eloquent\Ygt\ProcessFieldRelationSelect as ProcessFieldRelationSelectModel;
use App\Eloquent\Ygt\ProcessFieldRelationUnit as ProcessFieldRelationUnitModel;
use App\Eloquent\Ygt\ProcessField as ProcessFieldModel;

/**
 * Created by PhpStorm.
 * User: zhuyujun
 * Date: 2017/11/9
 * Time: 14:50
 */
class Order extends Model
{
    //工单列表
    public function getOrderList($where)
    {
        return OrderModel::where($where)->orderBy('id', 'desc')->get();
    }

    //工单详情
    public function getOrderDetail($input)
    {
        $orderProcessDetail = [];
        //获取工单信息
        $orderInfo = OrderModel::where('id', $input['order_id'])->first()->toArray();

        $orderFieldList = [
            'order_type' => '工单号/生产单号',
            'order_title' => '工单号/生产单号',
            'contract_number' => '合同编号',
            'customer_name' => '客户名称',
            'product_name' => '产品名称',
            'product_num' => '产品数量',
            'unit' => '数量单位',
            'grammage' => '克数',
            'chip_specification_length' => '片料规格-长',
            'chip_specification_width' => '片料规格-宽',
            'finished_specification' => '成品规格',
            'finished_weight' => '成品重量',
            'finished_date' => '交货日期',
            'weaving_type' => '织布类型',
            'plate_numbering' => '印版编号',
            'plate_position' => '印版位置',
            'plate_modification' => '印版修改',
            'public_plate' => '公用印版',
            'is_priority' => '是否优先',
            'enterprise_icon' => '企业图标',
            'production_case_diagram' => '生产实例图',
            'status' => '工单状态',
            'uid' => '创建工单的用户',
        ];

        $showList = [];
        foreach ($orderFieldList as $fieldValue => $fieldTitle){
            //通过字段类型不同处理
            $showTitle = $orderInfo[$fieldValue];//默认展示字段的内容
            //特殊字段处理
            if($fieldValue == 'status'){//状态值
                $showTitle = $this->getOrderStatusById($orderInfo[$fieldValue]);
            }
            if($fieldValue == 'uid'){//创建工单的用户
                $showTitle = $this->getUserNameById($orderInfo[$fieldValue]);
            }

            $showList[] = [
                'show_title' => $fieldTitle,
                'show_value' => $showTitle,
            ];

        }
        return $showList;
    }

    //主工单-通过状态id获取状态值
    public function getOrderStatusById($statusId)
    {
        $statusList = [
            '1' => '未接单',
            '2' => '生产中',
            '3' => '已完成',
            '4' => '待派发',
            '101' => '待开工',
        ];

        return $statusList[$statusId];
    }

    //通过用户id获取用户姓名
    public function getUserNameById($uid)
    {
        $userInfoObj = DepartmentUserModel::getInfo(['user_id' => $uid]);
        if ($userInfoObj) {
            $userInfo = DepartmentUserModel::getInfo(['user_id' => $uid])->toArray();
            return $userInfo['truename']."<br>".$userInfo['mobile'];
        } else {
            return false;
        }
    }

    //通过用户id获取用户姓名
    public function getOrderOrderTypeById($orderTypeId)
    {
        $orderTypeInfo = OrderTypeModel::withTrashed()->where(['id' => $orderTypeId])->first()->toArray();
        return $orderTypeInfo['title'];

    }

    //获取工序工单列表
    public function getOrderProcessList($where = [])
    {
        return OrderProcessModel::where($where)->orderBy('order_id', 'desc')->orderBy('id', 'desc')->get();
    }

    //获取工序工单列表
    public function getOrderProcessCourseList($where = [])
    {
        return OrderProcessCourseModel::where($where)->orderBy('order_process_id', 'desc')->orderBy('id', 'desc')->get();
    }

    //获取工序工单详情
    public function getOrderProcessDetail($input)
    {
        $orderProcessDetail = [];
        //获取对应工单工序表信息
        $orderProcessInfo = OrderProcessModel::where('id', $input['order_process_id'])->first()->toArray();
        //获取工单信息
        $orderInfo = OrderModel::where('id', $orderProcessInfo['order_id'])->first()->toArray();
        //获取工序名称
        $processTitle = ProcessMOdel::getOneValueById($orderProcessInfo['process_type'], 'title');

        $processFieldList = ProcessModel::getFieldListByProcessId($orderProcessInfo['process_type']);

        $showList = [];
        foreach ($processFieldList as $processFieldRow){
            //通过字段类型不同处理
            //获取字段对应的类型1=>'文本',2=>'文本域',3=>'下拉框-跳界面',4=>'跳材料',5=>'有单位',6=>'开关',7=>'时间'
            $tmpFiledType = ProcessFieldModel::getFieldTypeByFieldName($processFieldRow['field_name'],$orderInfo['company_id']);
            $showTitle = $orderProcessInfo[$processFieldRow['field_name']];//默认展示字段的内容
            if($tmpFiledType == 3){//下拉框
                $selectId = $orderProcessInfo[$processFieldRow['field_name']];
                $showTitle = ProcessFieldRelationSelectModel::getOneValueById( $selectId, 'title' );
            }elseif($tmpFiledType == 5){//有单位 数量+单位
                $tmpArr = explode(',',$orderProcessInfo[$processFieldRow['field_name']]);
                $showNum = $tmpArr[0];
                $unitId = $tmpArr[1];
                $showUnit = ProcessFieldRelationUnitModel::getOneValueById( $unitId, 'title' );
                $showTitle = $showNum.' '.$showUnit;
            }elseif($tmpFiledType == 6){//开关 0-否 1-是
                $switchValue = $orderProcessInfo[$processFieldRow['field_name']];
                $showTitle = $switchValue == 1 ? '是' : '否';
            }elseif($tmpFiledType == 4){//材料选择暂时不显示在
                continue;
            }

            $showList[] = [
                'show_title' => $processFieldRow['title'],
                'show_value' => $showTitle,
            ];

        }
        return $showList;
    }

    public function getOrderPreInfo($where){
        return OrderPreModel::where($where)->first();
    }






    //获取工序id对应的工序名称
    public function getProcessTitle($processId)
    {
        return $processTitle = ProcessModel::getOneValueById($processId, 'title');
    }


    //工单工序-通过状态id获取状态值
    public function getOrderProcessStatusById($statusId)
    {
        $statusList = [
            '1' => '未接单',
            '2' => '待分派',
            '3' => '已开工',
            '4' => '已完成',
            '101' => '待开工',
        ];
        return $statusList[$statusId];
    }


    //员工工单-通过状态id获取状态值
    public function getOrderProcessCourseStatusById($statusId)
    {
        $statusList = [
            '1' => '待开工',
            '2' => '待领材',
            '3' => '已开工',
            '4' => '已完成',
            '101' => '待开工',
        ];
        return $statusList[$statusId];
    }


}